document.write("<script src=\"" + context
		+ "/app/jlbd/js/sellist.js\"></script>");
document.write("<script src=\"" + context
		+ "/system/group/js/groupcombo4jsp.js\"></script>");
document.write('<link type="text/css" rel="stylesheet" href="' + context
		+ '/system/group/css/groupmanage.css"/>');

var querykey = 'list';
var pageSize = 15;
var showchk = false;
var ds;

var chkColumModel = [];
chkColumModel.push({
			'header' : '&nbsp;',
			'dataIndex' : 'PAGINATION_NUMBER',
			sortable : true,
			clazz : 'PAGINATION_NUMBER'
		});
chkColumModel.push({
			'header' : '监理姓名',
			'dataIndex' : 'NAME',
			sortable : true,
			clazz : '',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '性别',
			'dataIndex' : 'SEX',
			sortable : true,
			clazz : 'SEX',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '联系电话',
			'dataIndex' : 'PHONE',
			sortable : true,
			clazz : '',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '所在单位',
			'dataIndex' : 'GROUPNAME',
			sortable : true,
			clazz : '',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '设备编号',
			'dataIndex' : 'NU_MOBILECODE',
			sortable : true,
			clazz : '',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '监理标段',
			'dataIndex' : 'NU_BD',
			sortable : true,
			clazz : 'NU_BD',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '设计单元/标段',
			'dataIndex' : 'NU_GC',
			sortable : true,
			clazz : '',
			renderer : renderHeader
		});

var cm = new Ext.grid.ColumnModel(chkColumModel);

var fields = ['PAGINATION_NUMBER', 'NU_ID', 'NU_USERID', 'NU_BD', 'NU_GC',
		'NU_MOBILECODE', 'NAME', 'JOB', 'PHONE', 'SEX', 'GROUPNAME',
		'GROUPNAME', 'VALID'];

function initQueryList() {
	var toolbar = new Ext.Toolbar({
		id : querykey + '_toolbar',
		autoWidth : true,
		autoShow : true,
		items : [{
			iconCls : 'query',
			text : '数据查询',
			handler : function() {
				var cellist = querysellist('监管标段', 'nubdid', 'nubd', '', true);

				var queryForm = new Ext.FormPanel({
							labelWidth : 75, // label settings here
							// cascade
							// unless
							// overridden
							frame : false,
							bodyStyle : 'padding:5px 5px 0',
							width : 350,
							defaults : {
								width : 230
							},
							defaultType : 'textfield',

							items : [{
										fieldLabel : '姓名',
										id : 'cname',
										name : 'cname'
									}, {
										fieldLabel : '电话',
										id : 'cmobile',
										name : 'cmobile'
									}, {
										xtype : 'hidden',
										fieldLabel : '设备编号',
										id : 'cmobilecode',
										name : 'cmobilecode'
									}, {
										xtype : 'hidden',
										name : 'nubdid',
										id : 'nubdid'
									}, {
										xtype : 'hidden',
										name : 'nubd',
										id : 'nubd'
									}, {
										xtype : 'hidden',
										name : 'deptid',
										id : 'deptid'
									}, {
										xtype : 'hidden',
										name : 'deptname',
										id : 'deptname'
									}, {
										xtype : 'hidden',
										name : 'depttype',
										id : 'depttype'
									}]
						});

				if (roles.containsKey('NSManager')) {
					var groupCombo = getGroupSelectCombo4Single('deptid',
							'deptname', 'depttype', groupId, true);
					groupCombo.fieldLabel = '所属单位';
					queryForm.add(groupCombo);
					queryForm.doLayout();
				}

				var formUrl = context + '/system/result4form.jsp?1=1';

				if ($('cname_')) {
					formUrl += '&cname=' + $('cname_').value;
				}

				if ($('cmobile_')) {
					formUrl += '&cmobile=' + $('cmobile_').value;
				}

				if ($('nubdid1_')) {
					formUrl += '&nubdid=' + $('nubdid1_').value;
					formUrl += '&nubdid_=' + $('nubd1_').value;
					formUrl += '&nubd=' + $('nubd1_').value;
				}

				if ($('deptid1_')) {
					formUrl += '&deptid=' + $('deptid1_').value;
					formUrl += '&deptid_=' + $('deptname1_').value;
					formUrl += '&deptname=' + $('deptname1_').value;
					formUrl += '&depttype=' + $('depttype1_').value;
				}

				if ($('cmobilecode_')) {
					formUrl += '&cmobilecode=' + $('cmobilecode_').value;
				}

				queryForm.getForm().load({
							url : encodeURI(formUrl),
							waitTitle : '提示',
							waitMsg : '正在加载数据,请稍候...',
							animEl : "loding",
							success : function(form, action) {

							},
							failure : function(form, action) {
								Ext.Msg.alert('初始化查询项失败');
							}
						});

				var queryWin = new Ext.Window({
					renderTo : Ext.getBody(),
					layout : 'fit',
					width : 450,
					height : 300,
					title : '数据查询',
					resizable : true,
					plain : true,
					modal : true,

					items : [queryForm],

					buttons : [{
						id : 'objectQueryButton',
						text : '查 询',
						handler : function() {
							createHidden('cname_', queryForm.form
											.findField('cname').getValue());
							createHidden('cmobile_', queryForm.form
											.findField('cmobile').getValue());
							createHidden('cmobilecode_', queryForm.form
											.findField('cmobilecode')
											.getValue());
							createHidden('nubdid1_', queryForm.form
											.findField('nubdid').getValue());
							createHidden('nubd1_', queryForm.form
											.findField('nubd').getValue());
							createHidden('deptid1_', queryForm.form
											.findField('deptid').getValue());
							createHidden('deptname1_', queryForm.form
											.findField('deptname').getValue());
							createHidden('depttype1_', queryForm.form
											.findField('depttype').getValue());

							ds.reload({
										params : getQueryTestParams(),
										callback : function(r, options, success) {
											initInfoList(r, options, success);
										}
									});
							queryWin.close();
						}
					}, {
						text : '重 置',
						handler : function() {
							queryForm.form.reset();
						}
					}, {
						text : '关 闭',
						handler : function() {
							queryWin.close();
							// queryObjectTestWin =
							// null;
							// queryWin = null;
						}
					}]
				});

				queryWin.show(this);
			}
		}]
	});

	toolbar.addClass('ExtToolBar');

	toolbar.render('listToolbarArea');

	ds = new Ext.data.JsonStore({
				idProperty : 'PAGINATION_NUMBER',
				remoteSort : false,
				totalProperty : 'totalCount',
				root : 'queryList',
				fields : fields,
				proxy : new Ext.data.HttpProxy({
							url : context + '/app/svaction.do?method=querylist'
						})
			});

	/* 设置自定义参数 */
	ds.on('beforeload', function(thiz, options) {
				var params = getQueryTestParams();
				Ext.apply(thiz.baseParams, params);
			});

	storeOnLoad(0);
}

function getQueryTestParams(key) {
	return {
		start : 0,
		limit : pageSize,
		cname : $('cname_') ? $('cname_').value : '',
		cmobile : $('cmobile_') ? $('cmobile_').value : '',
		cmobilecode : $('cmobilecode_') ? $('cmobilecode_').value : '',
		nubdid : $('nubdid1_') ? $('nubdid1_').value : '',
		deptid : $('deptid1_') ? $('deptid1_').value : ''
	};
}

function storeOnLoad(start) {
	Ext.MessageBox.wait('加载数据过程中...');

	ds.load({
				params : {
					start : start,
					limit : pageSize
				},
				callback : function(r, options, success) {
					initInfoList(r, options, success);
				}
			});
}

function storeReload(start) {
	Ext.MessageBox.wait('加载数据过程中...');

	ds.reload({
				params : {
					start : start,
					limit : pageSize
				},
				callback : function(r, options, success) {
					initInfoList(r, options, success);
				}
			});
}

function initInfoList(r, options, success) {
	if (success == false) {
		Ext.Msg.alert('ERROR', '加载数据出现异常.');
	} else {
		var total = ds.getCount();// 数据行数

		while ($(querykey).rows.length > 1) {
			$(querykey).deleteRow($(querykey).rows.length - 1);
		}

		var arr = [];
		for (var j = 0; j < total; j++) {
			var record = ds.getAt(j);

			var tr = $(querykey).insertRow($(querykey).rows.length);

			var i = 0;
			Ext.each(chkColumModel, function(item) {
				var td = tr.insertCell(i);
				td.className = item.clazz;
				if (showchk && i == 0) {
					td.innerHTML = '<div class="divn"><input type="checkbox" name="chkitem" value="'
							+ j
							+ '">&nbsp;'
							+ renderHeader(record.data[item.dataIndex], cm,
									record, j, i, ds) + '</div>';
				} else {
					td.innerHTML = '<div class="divn">'
							+ renderHeader(record.data[item.dataIndex], cm,
									record, j, i, ds) + '</div>';
				}

				i++;
			});
		}

		var ys = ds.getTotalCount() % pageSize;

		var sh = parseInt(ds.getTotalCount() / pageSize);

		var totalPage = (ys == 0 ? sh : sh + 1);

		var pageNO = parseInt(options.params.start / options.params.limit) + 1;

		var str1 = '共' + ds.getTotalCount() + '条数据';

		if (ds.getTotalCount() > 0) {
			str1 += ',第' + pageNO + '/' + totalPage + '页';
		}

		$('pageDisplay').innerHTML = str1;

		var str = '';

		if (pageNO > 1) {
			str += '<a href="javascript:storeReload(\'0\');">首页</a>';
		} else if (totalPage > 1) {
			str += '首页';
		}

		if (pageNO > 1) {
			var start = pageSize * (pageNO - 2) + 1

			str += '&nbsp;<a href="javascript:storeReload(\'' + start
					+ '\');">上一页</a>';
		}

		if (pageNO < totalPage) {
			var start = pageSize * pageNO + 1

			str += '&nbsp;<a href="javascript:storeReload(\'' + start
					+ '\');">下一页</a>';
		}

		if (pageNO < totalPage) {
			var start = pageSize * (totalPage - 1) + 1

			str += '&nbsp;<a href="javascript:storeReload(\'' + start
					+ '\');">尾页</a>';
		} else if (pageNO == totalPage && pageNO > 1) {
			str += '尾页';
		}

		$('pageRoll').innerHTML = str;

		Ext.MessageBox.hide();
	}
}

function renderHeader(value, cellmeta, record, rowIndex, columnIndex, store) {
	var fieldName = cm.getDataIndex(columnIndex);
	if (fieldName == 'CAR_DATE') {
		return formatDate(value);
	} else if (fieldName == 'GROUPNAME') {
		return str = '<div title="' + value
				+ '" class="divn" style="width:190px;">' + value + '</div>';
	} else if (fieldName == 'NU_BD') {
		return str = '<div title="' + value
				+ '" class="divn" style="width:250px;">' + value + '</div>';
	} else if (fieldName == 'NU_GC') {
		return str = '<div title="' + value
				+ '" class="divn" style="width:145px;">' + value + '</div>';
	} else if (fieldName == 'SP') {
		var str = '<a href="javascript:handleStatus(\'' + record.data['CAR_ID']
				+ '\',\'' + record.data['CAR_NO']
				+ '\', \'-1\', \'停用\');">查看</a>';

		return str;
	}
	return value;
}